layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#mgrRescList',
        url : 'list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 15,
        limits : [10,15,20,25],
        id : "mgrRescListTable",
        cols : [[
            {field: 'rescId', title: '资源ID', align:"left"},
            {field: 'rescName', title: '资源名称', align:"left"},
            {field: 'orderId', title: '排序字段', align:'center'},
            {field: 'useflag', title: '是否启用', align:'center'},
            {title: '操作', width:170, templet:'#mgrRescListBar',fixed:"right",align:"center"}
        ]]
    });


    //添加文章
    function addMgrResc(){
        var index = layui.layer.open({
            title : "添加导航菜单",
            type : 2,
            content : "mgrRescAdd",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }

    function updateMgrResc(data){
        var index = layui.layer.open({
            title : "修改资源菜单",
            type : 2,
            content : "mgrRescUpdate?rescId="+data.rescId,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);

                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addMgrResc_btn").click(function(){
        addMgrResc();
    })

    //列表操作
    table.on('tool(mgrRescList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            updateMgrResc(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('删除后将导致系统运行不稳定，确定删除此菜单资源？',{icon:3, title:'提示信息'},function(index){
                $.post("delete",{
                    rescId : data.rescId
                },function(data){
                    data = JSON.parse(data)
                    if(data.flag ==="success"){
                        layer.msg("删除成功");
                        tableIns.reload();
                        layer.close(index);
                    }else{
                        layer.msg("删除失败！", {icon: 2});
                    }
                })
            });
        }
    });

})